//
//  MSCoinViewController.m
//  SocietySale
//
//  Created by Mike on 16/11/18.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSCoinViewController.h"
#import "MSCoinTableViewController.h"
#import "MSGetCoinViewController.h"
#import "MSMemberServeViewController.h"

@interface MSCoinViewController ()
@property (nonatomic, strong) MSCoinTableViewController *tableVC;
@property (nonatomic, strong) UILabel *balanceLabel;

@end

@implementation MSCoinViewController


- (void)initNavigationBar{
    [super initNavigationBar];
    leftBarBtn.hidden = NO;
    titleLable.text = @"江湖币";
}

- (void)initUI{
    self.tableVC = [[MSCoinTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    self.tableVC.view.frame = self.contentView.bounds;
    self.tableVC.enableRefresh = NO;
    self.tableVC.enableRequest = NO;
    self.tableVC.delegate = (id)self;
    self.tableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableVC.view];
    
    [self layoutHeaderView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [netEngine requestWalletWithTarget:self.VctrID hudView:self.contentView];
}

- (void)layoutHeaderView{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
    headerView.backgroundColor = kDefaultViewBackgroundColor;
    self.tableVC.tableView.tableHeaderView = headerView;
    
    UIView *horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, 0.6)];
    horizonLineView.backgroundColor = kNormalGray;
    [headerView addSubview:horizonLineView];
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    bgView.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:bgView];
    
    self.balanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 200, 50)];
    self.balanceLabel.font = HeitiFont(13);
    self.balanceLabel.textColor = kDarkGrayColor;
    [bgView addSubview:self.balanceLabel];
    
    [self refreshWallet];
    
    UIButton *getBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-90, 10, 80, 30)];
    getBtn.titleLabel.font = HeitiFont(14);
    [getBtn setTitle:@"获取换游币" forState:UIControlStateNormal];
    [getBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    getBtn.layer.cornerRadius = 2;
    getBtn.backgroundColor = UIColorFromRGB(0xcaa562);
    [getBtn addTarget:self action:@selector(getClick) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:getBtn];
}

- (void)testDataFunc{
    
}

- (void)updateViewController:(NSNotification *)note {
    NSDictionary *responseDic = note.userInfo;
    if (responseDic[Receiver]==self.VctrID) {
        if (responseDic[@"Wallet"]!=nil){
            _coinCount = responseDic[@"Wallet"];
            [self refreshWallet];
        }
    }
}

- (void)refreshWallet{
    NSMutableAttributedString *priceAttstr = [[NSMutableAttributedString alloc] initWithString:@"余额     X " attributes:@{NSFontAttributeName:HeitiFont(13),NSForegroundColorAttributeName:kDarkGrayColor}];
    
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    textAttachment.bounds = CGRectMake(0, -4, 17, 17);
    textAttachment.image = [UIImage imageNamed:@"coin"];
    NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment];
    [priceAttstr insertAttributedString:attachmentString atIndex:6];
    
    NSAttributedString *priStr = [[NSAttributedString alloc] initWithString:_coinCount attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor blackColor]}];
    [priceAttstr appendAttributedString:priStr];
    self.balanceLabel.attributedText = priceAttstr;
}

- (void)getClick{
    MSGetCoinViewController *vc = [[MSGetCoinViewController alloc] init];
    [rootNavigation pushViewController:vc animated:YES];
}

- (void)selectBtnWithIndex:(NSInteger)btnIndex{
    if (btnIndex == 0) {//去兑换,-> 即是会员服务
        MSMemberServeViewController *vc = [[MSMemberServeViewController alloc] init];
        vc.memberType = MemberType_temp;
        [rootNavigation pushViewController:vc animated:YES];
    }
    if (btnIndex == 1) {//发布交易
        MSReleaseTradeViewController *vc = [[MSReleaseTradeViewController alloc] init];
        [rootNavigation pushViewController:vc animated:YES];
    }
}

@end
